function varargout = skinmaker(varargin)
% SKINMAKER MATLAB code for skinmaker.fig
%      SKINMAKER, by itself, creates a new SKINMAKER or raises the existing
%      singleton*.
%
%      H = SKINMAKER returns the handle to a new SKINMAKER or the handle to
%      the existing singleton*.
%
%      SKINMAKER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SKINMAKER.M with the given input arguments.
%
%      SKINMAKER('Property','Value',...) creates a new SKINMAKER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before skinmaker_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to skinmaker_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help skinmaker

% Last Modified by GUIDE v2.5 30-Mar-2022 23:13:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @skinmaker_OpeningFcn, ...
                   'gui_OutputFcn',  @skinmaker_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before skinmaker is made visible.
function skinmaker_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to skinmaker (see VARARGIN)

% Choose default command line output for skinmaker
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes skinmaker wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = skinmaker_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


function refresh(handles)

axes(handles.axes5);
cla;
drawskin(handles.ia2, [], [], handles.slim, [0 0 0]);


function sliderchange(hObject, eventdata, handles)

r = uint8(round(get(handles.slider1, 'Value')));
g = uint8(round(get(handles.slider2, 'Value')));
b = uint8(round(get(handles.slider3, 'Value')));
set(handles.text5, 'String', r);
set(handles.text6, 'String', g);
set(handles.text7, 'String', b);
set(handles.text8, 'BackgroundColor', [r g b]);
set(handles.text8, 'UserData', [r g b]);


function clickonfig(hObject, eventdata, handles)

pos = round(eventdata.IntersectionPoint(1:2));
if get(handles.togglebutton4, 'Value')
    if hObject.CData(pos(2),pos(1))
        handles.ia2(pos(2),pos(1),:) = 0;
        hObject.CData(pos(2),pos(1)) = 0;
    else
        handles.ia2(pos(2),pos(1),4) = 255;
        hObject.CData(pos(2),pos(1)) = 255;
    end
elseif handles.ia2(pos(2),pos(1),4)
    color = get(handles.text8, 'UserData');
    handles.ia2(pos(2),pos(1),1:3) = color;
    hObject.CData(pos(2),pos(1),:) = color;
end
guidata(hObject, handles);


function mousedown(hObject, eventdata, handles)

fullpos = get(handles.axes2, 'CurrentPoint');
pos = round(fullpos(1,1:2));
if pos(1) < 1 || pos(1) > length(handles.ia2(1,:,1)) || pos(2) < 1 || pos(2) > length(handles.ia2(:,1,1))
    return;
end
mousemove(hObject, eventdata, handles);
set(hObject, 'WindowButtonMotionFcn', @(hObject, eventdata) mousemove(hObject, eventdata, guidata(hObject)));
set(hObject, 'WindowButtonUpFcn', @(hObject, eventdata) mouseup(hObject, eventdata, guidata(hObject)));


function mousemove(hObject, eventdata, handles)

fullpos = get(handles.axes2, 'CurrentPoint');
pos = round(fullpos(1,1:2));
if pos(1) < 1 || pos(1) > length(handles.ia2(1,:,1)) || pos(2) < 1 || pos(2) > length(handles.ia2(:,1,1))
    return;
end
if get(handles.togglebutton4, 'Value')
    if handles.ha2.CData(pos(2),pos(1))
        handles.ia2(pos(2),pos(1),:) = 0;
        handles.ha2.CData(pos(2),pos(1)) = 0;
    else
        handles.ia2(pos(2),pos(1),4) = 255;
        handles.ha2.CData(pos(2),pos(1)) = 255;
    end
elseif handles.ia2(pos(2),pos(1),4)
    color = get(handles.text8, 'UserData');
    handles.ia2(pos(2),pos(1),1:3) = color;
    handles.ha2.CData(pos(2),pos(1),:) = color;
end
guidata(hObject, handles);


function mouseup(hObject, eventdata, handles)

set(hObject, 'WindowButtonMotionFcn', '');
set(hObject, 'WindowButtonUpFcn', '');
refresh(handles);


function pushbutton3_Callback(hObject, eventdata, handles)

str = get(handles.edit1, 'String');
if ~isempty(str)
    if ~endsWith(str, '.png')
        str = [str '.png'];
    end
    imwrite(handles.ia2(:,:,1:3), str, 'png', 'Alpha', handles.ia2(:,:,4));
end


function togglebutton4_Callback(hObject, eventdata, handles)

if get(hObject, 'Value')
    handles.ha2.CData = handles.ia2(:,:,4);
else
    handles.ha2.CData = handles.ia2(:,:,1:3);
end
guidata(hObject, handles);


function pushbutton5_Callback(hObject, eventdata, handles)

str = get(handles.edit1, 'String');
if ~isempty(str)
    try
        if endsWith(str, '.png')
            [img, map, alpha] = imread(str, 'png');
            slim = false;
        else
            [img, map, alpha, slim] = fetchskin(str);
        end
    catch
        return;
    end
    if slim
        set(handles.togglebutton5, 'Value', 1);
        set(handles.togglebutton5, 'String', 'Arm: Slim');
    end
    handles.slim = slim;
    if handles.slim
        set(handles.togglebutton5, 'String', 'Arm: Slim');
    else
        set(handles.togglebutton5, 'String', 'Arm: Default');
    end
    img = getimg(img, map, alpha);
    for i = 1:length(img(:,1,1))
        for j = 1:length(img(1,:,1))
            if ~img(i,j,4)
                img(i,j,1:3) = 0;
            end
        end
    end
    handles.ia2 = img;
    if isfield(handles, 'ha2')
        if get(handles.togglebutton4, 'Value')
            handles.ha2.CData = handles.ia2(:,:,4);
        else
            handles.ha2.CData = handles.ia2(:,:,1:3);
        end
    else
        axes(handles.axes2);
        colormap('gray');
        handles.ha2 = imshow(img(:,:,1:3));
        set(handles.figure1, 'WindowButtonDownFcn', @(hObject, eventdata) mousedown(hObject, eventdata, guidata(hObject)));
        set(handles.pushbutton3, 'Enable', 'on');
        set(handles.togglebutton4, 'Enable', 'on');
        set(handles.togglebutton5, 'Enable', 'on');
    end
    axes(handles.axes5);
    axis equal;
    axis([-16 16 -16 16 0 40]);
    view([100 100 100]);
    refresh(handles);
    guidata(hObject, handles);
end


function togglebutton5_Callback(hObject, eventdata, handles)

handles.slim = ~handles.slim;
if handles.slim
    set(handles.togglebutton5, 'String', 'Arm: Slim');
else
    set(handles.togglebutton5, 'String', 'Arm: Default');
end
refresh(handles);
guidata(hObject, handles);